<template>
  <div class="repair">
    <div>
      <el-form :inline="true" :model="query" size="mini">
        <el-form-item label="">
          <el-input v-model="query.name" placeholder="设备名"></el-input>
        </el-form-item>
        <el-form-item label="">
          <el-input v-model="query.error" placeholder="异常项目"></el-input>
        </el-form-item>
        <el-form-item label="">
          <el-select v-model="query.status" placeholder="工单状态">
            <el-option
              v-for="item in status"
              :key="item"
              :label="item"
              :value="item || ''"
              >{{ item }}</el-option
            >
          </el-select>
        </el-form-item>
        <el-form-item label="">
          <el-select v-model="query.equip_status" placeholder="设备状态">
            <el-option
              v-for="item in estatus"
              :key="item"
              :label="item"
              :value="item || ''"
              >{{ item }}</el-option
            >
          </el-select>
        </el-form-item>
        <el-form-item>
          <el-button type="primary" icon="el-icon-search" @click="submitQuery"
            >查询</el-button
          >
          <el-button icon="el-icon-refresh" @click="resetQuery">重置</el-button>
        </el-form-item>
      </el-form>
    </div>
    <div style="text-align: left">
      <el-button
        icon="el-icon-plus"
        type="primary"
        size="mini"
        @click="addrepair"
        >添加</el-button
      >
    </div>
    <div>
      <el-table
        :data="tableData"
        v-loading="tableLoading"
        size="small"
        style="width: 100%"
        stripe
        border
      >
        <el-table-column
          prop="num"
          label="维修编号"
          width="130"
        ></el-table-column>
        <el-table-column
          prop="equip_num"
          label="设备编号"
          width="150"
        ></el-table-column>
        <el-table-column
          prop="name"
          label="设备名称"
          width="150"
        ></el-table-column>
        <el-table-column prop="equip_status" label="设备状态" width="100">
          <template v-slot="scope">
            <el-tag size="medium" v-if="scope.row.equip_status == '正常'">{{
              scope.row.equip_status
            }}</el-tag>
            <el-tag
              size="medium"
              type="danger"
              v-if="scope.row.equip_status == '故障'"
              >{{ scope.row.equip_status }}</el-tag
            >
            <el-tag
              size="medium"
              type="warning"
              v-if="scope.row.equip_status == '带病运行'"
              >{{ scope.row.equip_status }}</el-tag
            >
          </template>
        </el-table-column>
        <el-table-column
          prop="error"
          label="异常项目"
          width="200"
        ></el-table-column>
        <el-table-column
          prop="position"
          label="位置"
          width="200"
        ></el-table-column>
        <el-table-column
          prop="status"
          label="状态"
          style="text-align: center"
          width="80"
        >
          <template v-slot="scope">
            <el-tag
              style=""
              size="medium"
              type="success"
              v-if="scope.row.status == '已验收'"
              >{{ scope.row.status }}</el-tag
            >
            <el-tag
              size="medium"
              type="warning"
              v-if="scope.row.status == '已解决'"
              >{{ scope.row.status }}</el-tag
            >
            <el-tag
              style=""
              size="medium"
              type="danger"
              v-if="scope.row.status == '待处理'"
              >{{ scope.row.status }}</el-tag
            >
            <el-tag
              style=""
              size="medium"
              v-if="scope.row.status == '已接单'"
              >{{ scope.row.status }}</el-tag
            >
            <el-tag
              style=""
              size="medium"
              type="info"
              v-if="scope.row.status == '维修中'"
              >{{ scope.row.status }}</el-tag
            >
          </template>
        </el-table-column>
        <el-table-column
          prop="created_at"
          label="创建时间时间"
          width="160"
        ></el-table-column>
        <el-table-column label="操作" width="200">
          <template v-slot="scope">
            <el-button
              size="mini"
              icon="el-icon-edit"
              type="text"
              @click="editrepair(scope.$index, scope.row)"
              >编辑</el-button
            >
            <el-button
              size="mini"
              icon="el-icon-delete"
              type="text"
              @click="deleterepair(scope.$index, scope.row)"
              >删除</el-button
            >
            <el-button
              size="mini"
              icon="el-icon-circle-check"
              type="text"
              @click="check(scope.$index, scope.row)"
              >验收</el-button
            >
          </template>
        </el-table-column>
      </el-table>
    </div>
    <div style="text-align: center; margin-top: 10px">
      <el-pagination
        @size-change="handleSizeChange"
        @current-change="handleCurrentChange"
        :current-page="1"
        :page-sizes="[10, 20, 50, 100]"
        :page-size="10"
        layout="total, sizes, prev, pager, next, jumper"
        :total="tableDataTotal"
      ></el-pagination>
    </div>

    <el-dialog title="工单" v-model="editDialogShow" width="30%">
      <el-form
        ref="repairFormRules"
        :rules="repairFormRules"
        :model="repairForm"
        label-width="80px"
        size="mini"
      >
        <el-form-item label="设备编号">
          <el-select
            v-model="repairForm.equip_num"
            filterable
            remote
            reserve-keyword
            placeholder="请输入设备编号"
            :remote-method="remoteMethod"
            :loading="loading"
          >
            <el-option
              v-for="item in options"
              :key="item.value"
              :label="item.label"
              :value="item.value"
            >
            </el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="设备状态">
            <el-select v-model="repairForm.equip_status" placeholder="请选择">
              <el-option
                v-for="item in statuesOptions"
                :key="item"
                :label="item"
                :value="item">
              </el-option>
            </el-select>
        </el-form-item>
        <el-form-item label="异常项目">
          <el-input v-model="repairForm.error"></el-input>
        </el-form-item>
        <el-form-item label="位置">
          <el-input v-model="repairForm.position"></el-input>
        </el-form-item>
        <el-form-item style="text-align: right">
          <el-button type="primary" @click="onSubmit">确定</el-button>
        </el-form-item>
      </el-form>
    </el-dialog>
  </div>
</template>

<script>
import { page, create, update, repairDelete, checkrepair } from "../api/warranty";
import { query as querypage } from "../api/manage";
import dayjs from "dayjs";
export default {
  name: "User",
  data() {
    return {
      options: [{}],
      list: [],
      loading: false,
      states: [],
      query: { limit: 10, page: 1 },
      tableData: [{}],
      tableDataTotal: 0,
      tableLoading: true,
      editDialogShow: false,
      repairForm: { roles: [], name: [] },
      repairFormRules: {
        // username: [{ required: true, message: "请输入账号", trigger: "blur" }],
        // name: [{ required: true, message: "请输入姓名", trigger: "blur" }],
        // roles: [{ required: true, message: "请选择角色", trigger: "blur" }],
      },
      roles: [{}],
      isAdd: true,
      status: ["待处理", "已接单", "维修中", "已解决", "已验收"],
      flag: 0,
      statuesOptions:['带病运行','故障'],
      estatus:['带病运行','故障','正常']
    };
  },
  created() {
    this.page();
  },
  mounted() {
    this.querypage();
  },
  methods: {
    remoteMethod(query) {
      if (query !== "") {
        this.loading = true;
        setTimeout(() => {
          this.loading = false;
          this.options = this.list.filter((item) => {
            return item.label.toLowerCase().indexOf(query.toLowerCase()) > -1;
          });
        }, 200);
      } else {
        this.options = [];
      }
    },
    addrepair() {
      this.isAdd = true;
      this.editDialogShow = true;
      this.repairForm = { roles: [] };
    },
    editrepair(index, repair) {
      this.isAdd = false;
      this.editDialogShow = true;
      this.repairForm = { ...repair };
    },
    check(index, row) {
      this.$confirm(`验收编号【${row.num}】, 是否继续?`, "验收", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning",
      }).then(() => {
        checkrepair({equip_num:row.equip_num})
        .then((res)=>{
          if(res.success){
            this.$message({ type: "success", message: "验收成功!" });
              this.page();
            } else {
              this.$message.error("验收失败！");
            }
          })
          .catch(() => this.$message.error("验收失败！"));
        });
      this.page();
    },
    deleterepair(index, repair) {
      this.$confirm(`删除编号【${repair.num}】, 是否继续?`, "删除", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning",
      }).then(() => {
        repairDelete({ num: repair.num })
          .then((res) => {
            if (res.success) {
              this.$message({ type: "success", message: "删除成功!" });
              this.page();
            } else {
              this.$message.error("删除失败！");
            }
          })
          .catch(() => this.$message.error("删除失败！"));
      });
    },
    submitQuery() {
      this.page();
    },
    resetQuery() {
      this.query = { limit: 10, page: 1 };
      this.page();
    },
    onSubmit() {
      this.repairForm.buy_time = dayjs(this.repairForm.buy_time).format(
        "YYYY-MM"
      );
      this.repairForm.guarantee = dayjs(this.repairForm.guarantee).format(
        "YYYY-MM"
      );
      this.repairForm.commission = dayjs(this.repairForm.commission).format(
        "YYYY-MM"
      );
      this.repairForm.scrap = dayjs(this.repairForm.scrap).format("YYYY-MM");
      this.$refs.repairFormRules.validate((valid) => {
        if (valid) {
          if (this.isAdd) {
            this.create();
          } else {
            this.update();
          }
        } else {
          return false;
        }
      });
    },
    handleSizeChange(val) {
      this.query.limit = val;
      this.page();
    },
    handleCurrentChange(val) {
      this.query.page = val;
      this.page();
    },
    page() {
      this.tableLoading = true;
      page(this.query)
        .then((res) => {
          if (res.success) {
            this.tableData = res.data.rows;
            this.tableDataTotal = res.data.count;
            this.tableLoading = false;
          }
        })
        .catch(() => {
          this.tableLoading = false;
        });
    },
    querypage() {
      querypage().then((res) => {
        var array = [];
        console.log(res);
        for (var i = 0; i < res.data.rows.length; i++) {
          array.push(res.data.rows[i].num);
        }
        this.states = array;
        this.list = this.states.map((item) => {
          return { value: `${item}`, label: `${item}` };
        });
      });
    },
    create() {
      create(this.repairForm)
        .then((res) => {
          if (res.success) {
            this.$message({ message: "添加成功！", type: "success" });
            this.editDialogShow = false;
            this.page();
          } else {
            this.$message.error(res.data);
          }
        })
        .catch(() => this.$message.error("添加失败！"));
    },
    update() {
      update(this.repairForm)
        .then((res) => {
          if (res.success) {
            this.$message({ message: "修改成功！", type: "success" });
            this.editDialogShow = false;
            this.page();
          } else {
            this.$message.error(res.data);
          }
        })
        .catch(() => this.$message.error("修改失败！"));
    },
  },
};
</script>

<style scoped lang="scss">
// /deep/ .el-select--mini{
//     width: 426px;
// }
</style>